package com.regex.web.controller.gen;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regex.web.common.excel.ExportExcel;
import com.regex.web.entity.gen.DatabaseDTO;
import com.regex.web.entity.gen.TableInfoDTO;
import com.regex.web.service.gen.IDatabaseService;
import com.regex.web.service.gen.ITableInfoService;
import com.regex.web.utils.AjaxUtil;
import com.regex.web.utils.Contant;
import com.regex.web.utils.QueryResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ocean.ma
 * @since 2019-07-23
 */
@Controller
@RequestMapping("sys/database")
public class DatabaseController {

	private final static Logger logger = LoggerFactory.getLogger(DatabaseController.class);
	
	@Autowired
	private IDatabaseService IDatabaseService;
	
	@Autowired
	private ITableInfoService ITableInfoService;
	
	/**
	 * 分页查询
	 * @param page
	 * @param model
	 * @param databaseDTO
	 * @return
	 */
	@RequestMapping("show")
	public String show(Page<DatabaseDTO> page, Model model,@ModelAttribute("databaseDTO") DatabaseDTO databaseDTO) {
		try {
			QueryWrapper<DatabaseDTO> param = new QueryWrapper<DatabaseDTO>();
	        QueryResult<DatabaseDTO> result = IDatabaseService.page(page, param);
	        model.addAttribute("result", result);
        } catch (Exception e) {
			e.printStackTrace();
			logger.error("show page is Exception!");
		}
		return "sys/database/show";
	}
	
	/**
	 * 编辑/新增
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("input")
    public String toSave(String id, Model model) {
    	try {
	        if(StringUtils.isNotEmpty(id)) {
	        	DatabaseDTO databaseDTO = IDatabaseService.getById(id);
	            model.addAttribute("databaseDTO", databaseDTO);
	        }
        } catch (Exception e) {
			e.printStackTrace();
			logger.error("toSave page is Exception!");
		}
        return "sys/database/input";
    }
    
    /**
	 * 保存
	 * @param databaseDTO
	 * @param response
	 */
    @RequestMapping("save")
	public void save(DatabaseDTO databaseDTO, HttpServletResponse response) {
		boolean flag = false;
		try {
			if(StringUtils.isNotEmpty(databaseDTO.getId())) {
				databaseDTO.setUpdateTime(new Date());
				flag = IDatabaseService.updateById(databaseDTO);
			} else {
				databaseDTO.setIsDel(Contant.is_del_undelete);
				databaseDTO.setUpdateTime(new Date());
				databaseDTO.setCreateTime(new Date());
				flag = IDatabaseService.save(databaseDTO);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("save page is Exception!");
		}
		int n = 0;
		if(flag) {
			n = 1;
		}
		AjaxUtil.ajaxJsonSucMessage(response, n);
	}
	
	/**
	 * 详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("detial")
    public String detial(String id, Model model) {
        try {
	        if(StringUtils.isNotEmpty(id)) {
	        	DatabaseDTO databaseDTO = IDatabaseService.getById(id);
	            model.addAttribute("databaseDTO", databaseDTO);
	            
	            QueryWrapper<TableInfoDTO> param = new QueryWrapper<TableInfoDTO>();
	            param.eq("db_id", id);
		        List<TableInfoDTO> result = ITableInfoService.list(param);
		        model.addAttribute("result", result);
	        }
        } catch (Exception e) {
			e.printStackTrace();
			logger.error("detial page is Exception!");
		}
        return "sys/database/detial";
    }
	
	/**
	 * 删除
	 * @param id
	 * @param response
	 */
	@RequestMapping("del")
    public void del(String id, HttpServletResponse response) {
        int n = 0;
        try {
	        if(StringUtils.isNotEmpty(id)) {
	        	boolean flag = IDatabaseService.removeById(id);
	        	if(flag) {
	        		n = 1;
	        	}
	        } else {
	        	n = 2;
	        }
        } catch (Exception e) {
			e.printStackTrace();
			logger.error("del page is Exception!");
		}
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }
    
    /**
	 * 导出
	 * @param response
	 * @param databaseDTO
	 * @return
	 */
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(HttpServletResponse response, @ModelAttribute("databaseDTO") DatabaseDTO databaseDTO) {
        try {
            QueryWrapper<DatabaseDTO> param = new QueryWrapper<DatabaseDTO>(databaseDTO);
        	
        	String fileName = "数据"+(new Date()).getTime()+".xlsx";
            
            List<DatabaseDTO> datas = IDatabaseService.list(param);
            new ExportExcel("数据", DatabaseDTO.class).setDataList(datas).write(response, fileName).dispose();
            return null; 
        } catch (Exception e) {
        	logger.error("exportFile page is Exception!");
            e.printStackTrace();
        }
        return "redirect:show.htm";
    }
	
}
